package web.controller.sys;

import com.github.pagehelper.PageInfo;
import framework.exceptions.BusinessException;
import framework.security.AuthService;
import framework.utils.RequestUtil;
import framework.vo.ResultInfo;
import framework.vo.ResultList;
import io.swagger.annotations.Api;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import system.defined.FuncMoveType;
import system.entity.SysFunc;
import system.service.sys.SysFuncService;
import web.vo.MenuOption;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/sys/func")
@Api(tags = "功能管理")
public class SysFuncController {
    @Autowired
    private SysFuncService sysFuncService;
    @Autowired
    private AuthService authService;

    @PostMapping("/add")
    public ResultInfo<SysFunc> add(@ModelAttribute SysFunc param) {
        Date now = new Date();
        param.setCreateTime(now);
        param.setUpdateTime(now);

        if ("/sys/func/list".equals(param.getPid()) && new Integer(0).equals(param.getFunType())) {
            throw new BusinessException(RequestUtil.getMessageDefault("menu.operation.deny", "not allowed add the menu"));
        }

        if (StringUtils.isBlank(param.getId())) {
            if (StringUtils.isNotBlank(param.getFunPath())) {
                //以路径为ID
                param.setId(param.getFunPath());
            } else {
                //UUID
                param.setId(UUID.randomUUID().toString().replace("-", ""));
            }
        }

        if (StringUtils.isBlank(param.getPid())) {
            //默认顶级项
            param.setPid("-");
        }

        Integer added = sysFuncService.add(param);
        ResultInfo<SysFunc> info = new ResultInfo<>(param);
        return info;
    }

    @PostMapping("/del")
    public ResultInfo<Integer> del(@RequestParam("ids") List<String> ids) {
//        Integer deleted = sysFuncService.deleteByIds(ids);
        int deleted = 0;
        for (String id : ids) {

            if ("/sys/func/list".equals(id)
                    || "/sys/func/add".equals(id)
                    || "/sys/func/del".equals(id)
                    || "/sys/func/edit".equals(id)
                    || "/sys/func/tree".equals(id)
                    || "/sys/func/move".equals(id)) {
                throw new BusinessException(RequestUtil.getMessageDefault("menu.operation.deny", "not allowed change this menu"));
            }

            deleted += sysFuncService.deleteFunc(id);
        }
        ResultInfo<Integer> info = new ResultInfo<>(deleted);
        return info;
    }

    @PostMapping("/edit")
    public ResultInfo<Integer> edit(@ModelAttribute SysFunc param) {
        Date now = new Date();
        param.setUpdateTime(now);
        Integer updated = sysFuncService.update(param);
        ResultInfo<Integer> info = new ResultInfo<>(updated);
        return info;
    }

    @GetMapping("/info")
    public ResultInfo<SysFunc> info(@RequestParam("id") String id) {
        SysFunc info = sysFuncService.loadById(id);
        return new ResultInfo<>(info);
    }

    @GetMapping("/list")
    public ResultList<SysFunc> list(
            @RequestParam("name") String name,
            @RequestParam("pid") String pid,
            @RequestParam("funType") Integer funType,
            @RequestParam("pageIndex") Integer pageIndex,
            @RequestParam("pageSize") Integer pageSize) {
        SysFunc query = new SysFunc();
        query.setPid(pid);
        query.setName(name);
        query.setFunType(funType);
        PageInfo<SysFunc> info = sysFuncService.list(query, pageIndex, pageSize);
        return new ResultList<>(info);
    }

    /**
     * 移动节点位置
     *
     * @return
     */
    @PostMapping("/move")
    public ResultInfo<Integer> info(@RequestParam String id,
                                    @RequestParam String targetId,
                                    @RequestParam FuncMoveType moveType) {
        int moved = sysFuncService.move(id, targetId, moveType);
        return new ResultInfo<>(moved);
    }

    @GetMapping("/tree")
    public ResultList<MenuOption> tree() {
        List<SysFunc> funcList = sysFuncService.load(new SysFunc());
        List<MenuOption> menuOptions = MenuOption.tree(funcList, false);
        return new ResultList<MenuOption>(menuOptions);
    }

}